package com.ag.lwc.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ag.bean.TbEventCleanup;
import com.ag.bean.TbEventCleanupAttach;
import com.ag.dao.TbEventCleanupDAO;
//根据事件id查询该事件的事件善后
public class MyEventCleanupDAO extends TbEventCleanupDAO
{
	public List<TbEventCleanup> findCleanupByEventId(String eventId)
	{
		List<TbEventCleanup> result=null;
		Session session  =this.getSessionFactory().openSession();
		result=(List<TbEventCleanup>) session.createSQLQuery("select * from TB_EVENT_CLEANUP c left join tb_event t on c.E_ID = t.E_ID where t.E_ID =:eid").addEntity(TbEventCleanup.class).setString("eid", eventId).list();
	//初始化事件善后
		if(result!=null)
		  for(TbEventCleanup cleanUp:result)
		  {
			cleanUp.getTbEventCleanupAttachs().iterator();
			cleanUp.getEcTime();
			cleanUp.getTbEvent();
			cleanUp.getTbUser();
			if(cleanUp.getTbUser()!=null)
				cleanUp.getTbUser().getUName();
			for(TbEventCleanupAttach t:cleanUp.getTbEventCleanupAttachs())
			{
				t.getTaName();t.getTaAddress();
			}
			System.out.println("事件善后id："+cleanUp.getEcId()+"\n 事件善后内容："+cleanUp.getEcContent()+"\n事件善后用户："+cleanUp.getTbUser()+"  /n事件善后附件："+cleanUp.getTbEventCleanupAttachs());
		  }
		session.close();
		return result;
		
	}
	//根据事件善后id查询事件善后
	public TbEventCleanup findById(String cleanUpId)
	{
		TbEventCleanup cleanUp=null;
		Session session  =this.getSessionFactory().openSession();
		
		cleanUp= (TbEventCleanup) session.get(TbEventCleanup.class, cleanUpId);
		cleanUp.getTbEventCleanupAttachs().iterator();
		cleanUp.getEcTime();
		cleanUp.getTbEvent();
		cleanUp.getTbUser();
		System.out.println("事件善后id："+cleanUp.getEcId()+"\n 事件善后内容："+cleanUp.getEcContent()+"\n事件善后用户："+cleanUp.getTbUser().getUName());
		session.close();
		return cleanUp;
		
	}
	//增加事件善后
	public void addTbEventCleanup(TbEventCleanup eventCleanup,TbEventCleanupAttach cleanupAttach)
	{
		Session session  =this.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		if(eventCleanup==null) return;
		try{
			System.out.println("事件善后内容： "+eventCleanup.getEcContent());
			if(cleanupAttach!=null)
			{
				eventCleanup.getTbEventCleanupAttachs().add(cleanupAttach);
				cleanupAttach.setTbEventCleanup(eventCleanup);
				session.save(cleanupAttach);
				System.out.println("附件： "+cleanupAttach.getTaName());
			}
		
		System.out.println("事件善后的事件："+eventCleanup.getTbEvent().getEId());	
			session.save(eventCleanup);
			tx.commit();
			session.close();
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
			System.out.println("保存失败：");
			session.close();
		}
		
	}

}
